// Copyright 2006 Herre Kuijpers - <herre@xs4all.nl>
//
// This source file(s) may be redistributed, altered and custimized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
//-----------------------------------------------------------------------


using System;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Windows.Forms;


namespace EpisodeWizard.CoreUI
{

	/// <summary>
	/// Summary description for GradientPanel.
	/// </summary>
	public class GradientPanel : Panel
	{
		// declare the private members that will store the property values
		private float gradientAngle;
		private Brush brush = new SolidBrush(SystemColors.Control);

		/// <summary>
		/// Gets or sets the angle under which the gradient fill is displayed
		/// The best valid values to use is from 0-179 degrees.
		/// </summary>
		[Browsable( true ),
		DesignerSerializationVisibility( DesignerSerializationVisibility.Visible ),
		DefaultValue(0),
		Category("Appearance")]
		public float GradientAngle
		{
			get { return gradientAngle; }
			set { 
				gradientAngle = value; 
				CreateBrush();
			}
		}

		/// <summary>
		/// Gets or sets the start color of the gradient fill
		/// </summary>
		public override Color ForeColor
		{
			get	{ return base.ForeColor; }
			set {
				base.ForeColor = value;
				CreateBrush();
			}
		}

		/// <summary>
		/// Gets or sets the end color of the gradient fill
		/// </summary>
		public override Color BackColor
		{
			get { return base.BackColor; }
			set	{
				base.BackColor = value;
				CreateBrush();
			}
		}
		
		/// <summary>
		/// this is an optimization to (re)create the background paint brush
		/// only when its properties change, and not for every Paint event.
		/// </summary>
		private void CreateBrush()
		{
			//dispose of the brush if it exists
			if (brush != null)
				brush.Dispose();

			if (this.ClientRectangle.Width > 0)
			{
				// create the brush	
				brush = new LinearGradientBrush(this.ClientRectangle, ForeColor, BackColor, gradientAngle, true);

				// make sure the control refreshes itself and its child controls when the brush changes
				this.Invalidate(true);
			}
		}

		#region Constructor and initialization
		public GradientPanel()
		{
		}

		protected override void OnCreateControl()
		{
			CreateBrush();
			base.OnCreateControl ();
		}
		#endregion Constructor and initialization

		protected override void OnPaintBackground(PaintEventArgs pevent)
		{
			base.OnPaintBackground (pevent);
			pevent.Graphics.FillRectangle(brush, this.ClientRectangle);
		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize (e);
			CreateBrush();
		}

		protected override void OnBackColorChanged(EventArgs e)
		{
			base.OnBackColorChanged (e);
			CreateBrush();
		}
		
	}
}
